package com.mrlv.ht.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mrlv.ht.pojo.Dept;
import com.mrlv.ht.pojo.DeptExample;
import com.mrlv.ht.pojo.User;
import com.mrlv.ht.pojo.UserInfo;
import com.mrlv.ht.service.IDeptService;
import com.mrlv.ht.service.IUserService;
import com.mrlv.ht.utils.Enctype;

@Controller
public class UserController {
	
	@Resource
	private IUserService userService;
	@Resource
	private IDeptService deptService;
	
	@RequestMapping("/sysadmin/user/list")
	public String list(Model model) {
		List<User> users = userService.findAllUser(); 
		model.addAttribute("users", users);
		return "/sysadmin/user/jUserList";
	}
	
	@RequestMapping("/sysadmin/user/tocreate")
	public String tocreate(Model model) {
		List<Dept> depts = deptService.selectByExample(new DeptExample());
		List<User> users = userService.findAllUser();
		model.addAttribute("users", users);
		model.addAttribute("depts", depts);
		return "/sysadmin/user/jUserCreate";
	}
	
	@RequestMapping("/sysadmin/user/insert")
	/*
	 * 注意：用户新增的时候，有两个日期，一个是入职日期，一个是生日，SprigMVC默认的日期格式是 yyyy/MM/dd
	 * 但是页面用的是 yyyy-MM-dd 这种方式，所以需要用格式日期注解来转换
	 * 第一种方式：是用@TimeFormat注解，放到私有属性上，然后设置对应的pattren属性
	 * 第二种方式：用@InitBinder 日期格式类型转换器来做
	 */
	//可以采用name="userInfo.salary"来接收.亦或者采用两个类来接受
	public String insert(User user, UserInfo userInfo) {
		String username = user.getUsername();
		String password = user.getPassword();
		user.setPassword(Enctype.md5(password, username));
		userService.insert(user, userInfo);
		return "redirect:/sysadmin/user/list";
	}
	
	@RequestMapping("/sysadmin/user/toupdate")
	public String toupdate(Model model) {
		model.addAttribute("err", "该功能尚未完善");
		return "forward:/sysadmin/user/list";
	}
	
	/**
	 * 需要注意， 不仅要删除user_p表的数据，还要删除user_info_p表信息
	 * 避免出现僵尸数据
	 */
	@RequestMapping("/sysadmin/user/delete")
	public String delete(String[] userId) {
		if(userId == null) {
			return "redirect:/sysadmin/user/list";
		}
		int a = userService.deleteBatch(userId);
		int b = userService.deleteUserBatch(userId);
		System.out.println(a + "," + b);
		return "redirect:/sysadmin/user/list";
	}
	
	@RequestMapping("/sysadmin/user/stop") 
	public String stop(String userId) {
		if(userId == null) {
			return "redirect:/sysadmin/user/list";
		}
		User user = new User();
		user.setUserId(userId);
		user.setState(new BigDecimal(0));
		userService.updateByPrimaryKeySelective(user);	
		return "redirect:/sysadmin/user/list";
	}
	@RequestMapping("/sysadmin/user/start") 
	public String start(String userId) {
		if(userId == null) {
			return "redirect:/sysadmin/user/list";
		}
		User user = new User();
		user.setUserId(userId);
		user.setState(new BigDecimal(1));
		userService.updateByPrimaryKeySelective(user);
		return "redirect:/sysadmin/user/list";
	}
	
	@RequestMapping("/sysadmin/user/torole")
	public String torole() {
		return "/sysadmin/user/jUserRole";
	}
	
	
	
	/*
	 * 日期
	 */
	@InitBinder  //更改日期格式为yyyy-MM-dd
	public void InitBinder (ServletRequestDataBinder binder){
		binder.registerCustomEditor(
			java.util.Date.class, 
			new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
	}
}
